// pages/food/order.js
var WxParse = require('../../wxParse/wxParse.js');
import {
  gourmet,
  user
} from '../../api/apiData'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    info: '',
    id: '',
    quantity: 1,
    showCoupon: false,
    listCou: [],
    radio: '',
    couItem: "",
    priceSum: '',
    couIndex: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
    this.getDetail()
    this.getList()
  },
  onChange(e) {
    this.setData({
      quantity: e.detail,
    })

    this.getList()
  },

  onClick(event) {
    const {
      id,
      item,
      index
    } = event.currentTarget.dataset;
    this.setData({
      radio: id,
      coupon_id: id,
      couItem: item,
      couIndex: index,
      priceSum: this.data.info.price * this.data.quantity - Number(item.amount)
    });
    // this.toConOrder()
  },
  toCancelCou(){
    this.setData({
        radio:'',
        coupon_id:'',
        couItem:'',
        couIndex:'',
        showCoupon: false
    })
},
  toShowCou() {
    this.setData({
      showCoupon: true
    })
  },
  onCloseCou() {
    this.setData({
      showCoupon: false
    })
  },
  getList() {
    user.coupon.usableList({
      use_scene: 4,
      gourmet_product_id: this.data.id,
      quantity: this.data.quantity
    }, res => {
      if (res.code == 1) {
        this.setData({
          listCou: res.data
        })
        if(res.data.length>0){
          this.setData({
              couItem:res.data[0],
              radio:res.data[0].id,
              coupon_id:res.data[0].id,
              priceSum:Number(this.data.info.price*this.data.quantity)-Number(res.data[0].amount)
          })
      }
        // if (this.data.couItem != '') {
        //   this.setData({
        //     priceSum: this.data.info.price * this.data.quantity - Number(res.data[this.data.couIndex].amount)
        //   })
        // }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  onChangeCou(e) {
    // this.setData({
    //     radio: e.detail,
    // });
  },
  toComCou() {
    this.setData({
      showCoupon: false
    })
  },
  toPre(e) {
    let imgs = e.currentTarget.dataset.imgs
    let index = e.currentTarget.dataset.index
    wx.previewImage({
      urls: imgs,
      current: index
    })
  },
  getDetail() {
    gourmet.product.detail({
      product_id: this.data.id
    }, res => {
      if (res.code == 1) {
        this.setData({
          info: res.data,
          bannerList: [res.data.banner]
        })
        WxParse.wxParse('bindName', 'html', res.data.use_description, this, 5)
        WxParse.wxParse('bindName2', 'html', res.data.content, this, 5)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  toOrder() {
    // wx.navigateTo({
    //   url: '/pages/food/orderInfo',
    // })
    let that = this
    let obj = {
      product_id: this.data.id,
      quantity: this.data.quantity,
      user_coupon_id: this.data.coupon_id
    }
    gourmet.order.submit(obj, res => {
      if (res.code == 1) {
        wx.showToast({
          title: res.msg,
        })
        that.setData({
          order_id: res.data.id,
          order_no: res.data.order_no,
        })
        that.toPay()
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  // 支付
  toPay() {
    let order_no = this.data.order_no
    wx.login({
      success(res) {
        var code = res.code
        wx.showLoading({
          title: "请稍后...",
          mask: true
        })
        gourmet.order.pay({
          order_no,
          code
        }, res => {
          let ret_code = res.code
          let ret_msg = res.msg
          if (ret_code != 1) {
            //   Toast(ret_msg)
            wx.showToast({
              title: ret_msg,
              icon: 'error'
            })
            return
          }
          let {
            nonceStr,
            paySign,
            signType,
            timeStamp
          } = res.data
          wx.requestPayment({
            nonceStr,
            package: res.data.package,
            paySign,
            timeStamp,
            signType,
            success(res) {
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 1500
              });
              setTimeout(() => {
                wx.reLaunch({
                  url: '/pages/order/index?active=2',
                })
              }, 1000)
            },
            fail(res) {
              wx.showToast({
                title: '支付失败',
                duration: 1500,
                icon: 'error'
              });
              setTimeout(() => {
                wx.redirectTo({
                  url: '/pages/order/index?active=2',
                })
              }, 1000)

            }
          })
        })
      }
    })
  },
  toCoupon() {
    wx.navigateTo({
      url: '/pages/coupon/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})